<template>
  <page-header-wrapper :title="false">
    <a-card title="提交官方">
      <div class="table-page-search-wrapper">
        <a-form-model
          layout="inline"
          :model="queryParam"
        >
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="合同编号">
                <a-input v-model="queryParam['filter[contract_no]']" placeholder="请输入合同编号" :allowClear="true" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="业务编号">
                <a-input v-model="queryParam['filter[business_no]']" placeholder="请输入业务编号" :allowClear="true" />
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="客户电话">
                  <a-input v-model="queryParam['filter[phone]']" placeholder="请输入客户电话" :allowClear="true" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="业务名称">
<!--                  <a-select v-model="queryParam['filter[business_name]']" placeholder="请选择业务名称" :allowClear="true">-->
<!--                    <a-select-option v-for="(item, index) in businessOpts" :key="index" :value="item.name">{{item.name}}</a-select-option>-->
<!--                  </a-select>-->
                  <a-input v-model="queryParam['filter[business_name]']" placeholder="请输入业务名称" :allowClear="true" />
                </a-form-model-item>
              </a-col>
<!--              <a-col :md="6" :sm="24">-->
<!--                <a-form-model-item label="商标分类">-->
<!--                  <a-select v-model="queryParam['filter[sb_category]']" placeholder="请选择商标分类" :allowClear="true">-->
<!--                    <a-select-option v-for="(item, index) in categoryOpts" :key="index" :value="index">{{item}}</a-select-option>-->
<!--                  </a-select>-->
<!--                </a-form-model-item>-->
<!--              </a-col>-->
              <a-col :md="6" :sm="24">
                <a-form-model-item label="申请主体">
                  <a-input v-model="queryParam['filter[subject]']" placeholder="请输入申请主体" :allowClear="true" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="流程状态">
                  <a-select v-model="queryParam['filter[status2]']" placeholder="请选择流程状态" :allowClear="true">
                    <a-select-option v-for="(item, index) in statusOpts" :key="index" :value="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <!--              <a-col :md="6" :sm="24">-->
              <!--                <a-form-item label="领取时间">-->
              <!--                  <a-range-picker @change="onChangeReceive" />-->
              <!--                </a-form-item>-->
              <!--              </a-col>-->
              <!--              <a-col :md="6" :sm="24">-->
              <!--                <a-form-model-item label="所属顾问">-->
              <!--                  <a-select-->
              <!--                    show-search-->
              <!--                    v-model="queryParam['filter[adviser_uid]']"-->
              <!--                    placeholder="输入要搜索的顾问姓名"-->
              <!--                    :default-active-first-option="false"-->
              <!--                    :show-arrow="false"-->
              <!--                    :filter-option="false"-->
              <!--                    :not-found-content="null"-->
              <!--                    @search="onSearch"-->
              <!--                  >-->
              <!--                    <a-select-option v-for="(item,index) in searchSource" :key="index" :value="index">-->
              <!--                      {{ item }}-->
              <!--                    </a-select-option>-->
              <!--                  </a-select>-->
              <!--                </a-form-model-item>-->
              <!--              </a-col>-->
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
                <span class='table-page-search-submitButtons' :style="advanced && { float: 'right', overflow: 'hidden' } || {}">
                  <a-button type="primary" @click="$refs.table.refresh(true)">搜索</a-button>
                  <a-button style="margin-left: 8px" @click="reset">重置</a-button>
                  <a @click="toggleAdvanced" style="margin-left: 8px">
                    {{ advanced ? '收起' : '展开' }}
                    <a-icon :type="advanced ? 'up' : 'down'" />
                  </a>
                </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operator">
        <s-table
          bordered
          ref="table"
          :row-key="(record) => record.cb_id"
          :columns="governmentColumns"
          :data="governmentData"
        >
          <!--          <template slot="business_no" slot-scope="text">-->
          <!--            <a-popover trigger="hover">-->
          <!--              <template slot="content">-->
          <!--                <p>业务编号：{{text}}<a-divider type="vertical" /><a-icon class="copy" :data-clipboard-text="text" type="copy" @click="copy" /></p>-->
          <!--              </template>-->
          <!--              {{text}}-->
          <!--            </a-popover>-->
          <!--          </template>-->
          <template slot="contract_no" slot-scope="text,record">
            <a-popover trigger="hover">
              <template slot="content">
                <p>合同编号：{{text}}<a-divider type="vertical" /><a-icon class="copy" :data-clipboard-text="text" type="copy" @click="copy" /></p>
                <p>业务编号：{{record.business_no}}<a-divider type="vertical" /><a-icon class="copy" :data-clipboard-text="record.business_no" type="copy" @click="copy" /></p>
              </template>
              {{text}}<br>
              <a @click="toDetail(record.cb_id)">{{record.business_no}}</a>
            </a-popover>
          </template>
          <template slot="adviser" slot-scope="text, record">
            <a-tag>{{text}}</a-tag><br>
            <a-tag>{{record.group_type}}组</a-tag><br>
            <a-tag>{{record.leader}}</a-tag>
          </template>
          <template slot="sb_name" slot-scope="text, record">
            {{text}}{{record.bq_name}}
          </template>
          <template slot="phone" slot-scope="text">
            <a-popover trigger="hover">
              <template slot="content">
                <p>{{text}}<a-divider type="vertical" /><a-icon class="copy" :data-clipboard-text="text" type="copy" @click="copy" /></p>
              </template>
              {{text}}
            </a-popover>
          </template>
          <template slot="status2" slot-scope="text, record">
            <a-button icon="edit" v-if="record.status2_code == '20' || record.status2_code == '22' || record.status2_code == '30' || record.status2_code == '32'" size="small" type="danger" @click="changeStatus(record.cb_id)">{{text}}</a-button>
            <a-button v-else size="small" type="primary" @click="changeStatus(record.cb_id)">{{text}}</a-button>
          </template>
          <template slot="history" slot-scope="text, record">
            <a @click="historyModal(record.cb_id)">{{text}}</a>
          </template>
          <template slot="action" slot-scope="text, record">
            <a-button type="primary" size="small" @click="down(record.cb_id)">下载</a-button>
            <a-button type="primary" size="small" @click="toDetail(record.cb_id)">查看</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
    <a-modal
      centered
      title="材料下载"
      v-model="downloadVisible"
    >
      <a-form-model
        ref="downloadForm"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >
        <a-form-model-item label="业务名称">
          <a-input disabled :value="downForm.business_name" type="text" />
        </a-form-model-item>
        <a-form-model-item label="下载材料">
          <a @click="download(downForm.mat_file, downForm.mat_filename)">{{downForm.mat_filename}}</a>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
    <a-modal
      centered
      v-model="historyVisible"
      title="历史信息"
      :footer="null"
    >
      <a-timeline>
        <a-timeline-item v-for="(item, index) in historyData" :key="index">
          {{item.step}}{{item.username}},{{item.notes}} {{item.created_at}}
        </a-timeline-item>
      </a-timeline>
    </a-modal>
    <a-modal
      centered
      v-model="submitVisible"
      title="编辑状态"
      @ok="submitChangeStatus"
      @cancel="onCancel"
    >
      <a-form-model
        ref="ruleForm"
        :model="changeStatusForm"
      >
        <a-form-model-item prop="status2">
          <a-radio-group v-model="changeStatusForm.status2" @change="onChangeStatus">
            <a-radio :disabled="index == 34" v-for="(item, index) in changeStatusOpts" :key="index" :value="index">
              {{item}}
            </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item prop="notes">
          <a-textarea :rows="4" v-model="changeStatusForm.notes" placeholder="备注：材料反馈，请填写原因，不能为空"></a-textarea>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
  import Clipboard from 'clipboard'
  import { STable } from '@/components'
  import { getOffficial, getHistory, getOfficialEdit, postOfficialEdit, getDownloadMaterial } from '@/api/four.process'
  import fourColumns from '@/columns/four.process'
  import apis from '@/api'

  export default {
    components: {
      STable
    },
    name: 'Government',
    data () {
      return {
        labelCol: { span: 4 },
        wrapperCol: { span: 16 },
        advanced: true,
        queryParam: {},
        governmentColumns: fourColumns.government,
        historyVisible: false,
        submitVisible: false,
        downloadVisible: false,
        changeStatusOpts: {},
        categoryOpts: {},
        statusOpts: {},
        businessOpts: {},
        downForm: {},
        governmentData: parameters => {
          return getOffficial(Object.assign(parameters, this.queryParam)).then(res => {
            this.categoryOpts = res.result.sbCategoryOpts
            this.statusOpts = res.result.status2_opts
            this.businessOpts = res.result.businesses
            return res.result
          })
        },
        historyData: [],
        searchSource: [],
        changeStatusForm: {}
      }
    },
    methods: {
      changeStatus (id) {
        const app = this
        app.changeStatusForm = {}
        app.changeStatusForm['cb_id'] = id
        getOfficialEdit(id).then(res => {
          if (res.status === 0) {
            app.changeStatusOpts = res.result.status2_items
            app.changeStatusForm.notes = res.result.contract_business.notes
            app.submitVisible = true
          } else {
            app.$warning({
              title: res.msg
            })
            return false
          }
        })
      },
      submitChangeStatus () {
        const app = this
        const data = {}
        data['contract_business[status2]'] = app.changeStatusForm.status2
        data['contract_business[notes]'] = app.changeStatusForm.notes
        postOfficialEdit(app.changeStatusForm['cb_id'], data).then(res => {
          if (res.status === 0) {
            app.onCancel()
            app.$refs.table.refresh()
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      },
      onCancel () {
        this.changeStatusForm = {}
        this.submitVisible = false
      },
      onChangeStatus (e) {
        this.changeStatusForm.status2 = e.target.value
      },
      historyModal (id) {
        const app = this
        app.historyVisible = true
        getHistory(id).then(res => {
          if (res.status === 0) {
            app.historyData = res.result.data
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      reset () {
        this.queryParam = {}
        this.$refs.table.refresh(true)
      },
      // onSearch (searchText) {
      //   onCompletionAdviser(searchText).then(res => {
      //     this.searchSource = res.result.data
      //   })
      // },
      down (id) {
        const app = this
        getDownloadMaterial(id).then(res => {
          if (res.status === 0) {
            app.downForm = res.result
            app.downloadVisible = true
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      },
      download (value, filename) {
        window.open(apis.Download + '?value=' + value + '&filename=' + filename)
      },
      toDetail (id) {
        this.$router.push({ name: 'FourEdit', params: { id } })
      },
      copy () {
        const clipboard = new Clipboard('.copy')
        clipboard.on('success', e => {
          this.$message.success('复制成功')
        })
        clipboard.on('error', e => {
          this.$message.error('复制失败')
        })
      }
    }
  }
</script>

<style scoped>

</style>
